import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { DemoController } from './demo/demo.controller'
import { DemoModule } from './demo/demo.module'
import { UserModule } from './user/user.module'
import { AppService2 } from './app.service2'
import { ListModule } from './list/list.module'
import { ConfigModule } from './config/config.module'
import { UploadModule } from './upload/upload.module'
import { PModule } from './p/p.module'
import { LoginModule } from './login/login.module'
import { GuardModule } from './guard/guard.module'
import { TypeOrmModule } from '@nestjs/typeorm'
import { TestPModule } from './test-p/test-p.module';
import { CrudModule } from './crud/crud.module';

@Module({
  imports: [
    DemoModule,
    UserModule,
    ListModule,
    ConfigModule.forRoot({
      path: '/get',
    }),
    UploadModule,
    PModule,
    LoginModule,
    GuardModule,
    TypeOrmModule.forRoot({
      type: 'mysql', //数据库类型
      username: 'root', //账号
      password: '123456', //密码
      host: 'localhost', //host
      port: 3306, //
      database: 'nestjs', //库名
      // entities: [__dirname + '/**/*.entity{.ts,.js}'], //实体文件
      synchronize: true, //synchronize字段代表是否自动将实体类同步到数据库
      retryDelay: 500, //重试连接数据库间隔
      retryAttempts: 10, //重试连接数据库的次数
      autoLoadEntities: true, //如果为true,将自动加载实体 forFeature()方法注册的每个实体都将自动添加到配置对象的实体数组中
    }),
    TestPModule,
    CrudModule,
  ],
  controllers: [AppController, DemoController],
  // providers: [AppService],//这种是一个简写
  providers: [
    AppService2,
    {
      provide: 'ABC',
      useClass: AppService,
    },
    {
      provide: 'Test',
      useValue: ['孙悟空', '猪八戒', '资本家'],
    },
    {
      provide: 'FFF',
      inject: [AppService2],
      useFactory(appService2: AppService2) {
        console.log('appService2.getHello()', appService2.getHello())
        return appService2.getHello()
      },
    },
    {
      provide: 'SSS',
      inject: [AppService2],
      async useFactory(appService2: AppService2) {
        return await new Promise((res) => {
          setTimeout(() => {
            res(appService2.getHello())
          }, 3000)
        })
      },
    },
  ],
})
export class AppModule {}
